<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="枚举类什么是枚举类？枚举类是一种特殊的类，用于表示一组预定义的常量。 枚举在曰常生活中很常见，例如一个人的性别只能是“男”或者“女”，一周的星期只能是 7 天中的一个等。类似这种当一个变量有几种固定可能的取值时，就可以将它定义为枚举类型。 声明枚举类声明枚举时必须使用 enum 关键字，然后定义枚举的名称、可访问性、基础类型和成员等。如下，声明一个性别枚举类： 123enum Gender {">
<meta property="og:type" content="article">
<meta property="og:title" content="Java：枚举类和反射">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/05/08/Java%EF%BC%9A%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%92%8C%E5%8F%8D%E5%B0%84/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="枚举类什么是枚举类？枚举类是一种特殊的类，用于表示一组预定义的常量。 枚举在曰常生活中很常见，例如一个人的性别只能是“男”或者“女”，一周的星期只能是 7 天中的一个等。类似这种当一个变量有几种固定可能的取值时，就可以将它定义为枚举类型。 声明枚举类声明枚举时必须使用 enum 关键字，然后定义枚举的名称、可访问性、基础类型和成员等。如下，声明一个性别枚举类： 123enum Gender {">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-05-08T13:14:11.000Z">
<meta property="article:modified_time" content="2023-06-27T11:02:39.587Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-Java基础 -枚举类 -反射">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/05/08/Java%EF%BC%9A%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%92%8C%E5%8F%8D%E5%B0%84/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/05/08/Java%EF%BC%9A%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%92%8C%E5%8F%8D%E5%B0%84/","path":"2023/05/08/Java：枚举类和反射/","title":"Java：枚举类和反射"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java：枚举类和反射 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E7%B1%BB"><span class="nav-text">枚举类</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%9E%9A%E4%B8%BE%E7%B1%BB%EF%BC%9F"><span class="nav-text">什么是枚举类？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E6%9E%9A%E4%B8%BE%E7%B1%BB"><span class="nav-text">声明枚举类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E7%B1%BB%E4%B8%8Eswitch%E8%AF%AD%E5%8F%A5"><span class="nav-text">枚举类与switch语句</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E7%B1%BB%E7%9A%84%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E3%80%81%E6%96%B9%E6%B3%95%E5%92%8C%E6%9E%84%E9%80%A0%E5%99%A8"><span class="nav-text">枚举类的成员变量、方法和构造器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Enum%E7%B1%BB%E7%9A%84valueOf-%E5%92%8C%E5%85%B7%E4%BD%93%E6%9E%9A%E4%B8%BE%E7%B1%BB%E7%9A%84valueOf-%E6%96%B9%E6%B3%95%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">Enum类的valueOf()和具体枚举类的valueOf()方法的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E7%B1%BB%E6%96%B9%E6%B3%95"><span class="nav-text">枚举类方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#values-%E6%96%B9%E6%B3%95"><span class="nav-text">values()方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#valueOf-%E6%96%B9%E6%B3%95%E4%B8%8EcompareTo-%E6%96%B9%E6%B3%95"><span class="nav-text">valueOf()方法与compareTo()方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ordinal-%E6%96%B9%E6%B3%95"><span class="nav-text">ordinal()方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E7%B1%BB%E4%B8%8E%E6%99%AE%E9%80%9A%E7%B1%BB%E5%8C%BA%E5%88%AB"><span class="nav-text">枚举类与普通类区别</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%8D%E5%B0%84"><span class="nav-text">反射</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/05/08/Java%EF%BC%9A%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%92%8C%E5%8F%8D%E5%B0%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java：枚举类和反射 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java：枚举类和反射
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-08 21:14:11" itemprop="dateCreated datePublished" datetime="2023-05-08T21:14:11+08:00">2023-05-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-06-27 19:02:39" itemprop="dateModified" datetime="2023-06-27T19:02:39+08:00">2023-06-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-后端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="枚举类"><a href="#枚举类" class="headerlink" title="枚举类"></a>枚举类</h1><h2 id="什么是枚举类？"><a href="#什么是枚举类？" class="headerlink" title="什么是枚举类？"></a>什么是枚举类？</h2><p>枚举类是一种特殊的类，用于表示一组预定义的常量。</p>
<p>枚举在曰常生活中很常见，例如一个人的性别只能是“男”或者“女”，一周的星期只能是 7 天中的一个等。类似这种当一个变量有几种固定可能的取值时，就可以将它定义为枚举类型。</p>
<h2 id="声明枚举类"><a href="#声明枚举类" class="headerlink" title="声明枚举类"></a>声明枚举类</h2><p>声明枚举时必须使用 enum 关键字，然后定义枚举的名称、可访问性、基础类型和成员等。如下，声明一个性别枚举类：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Gender</span> {</span><br><span class="line">    MALE,FEMALE;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>之后便可以通过枚举类型名直接引用常量，如 <code>Gender.MALE</code>或<code>Gender.FEMALE</code>。</p>
<h2 id="枚举类与switch语句"><a href="#枚举类与switch语句" class="headerlink" title="枚举类与switch语句"></a>枚举类与switch语句</h2><p>使用枚举还可以使 switch 语句的可读性更强，例如以下示例代码：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Gender</span> {</span><br><span class="line">    MALE,FEMALE;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EnumTest</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">Gender</span> <span class="variable">gender</span> <span class="operator">=</span> Gender.MALE;</span><br><span class="line">        <span class="keyword">switch</span>(gender) {</span><br><span class="line">            <span class="keyword">case</span> MALE:</span><br><span class="line">                System.out.println(<span class="string">"男"</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> FEMALE:</span><br><span class="line">                System.out.println(<span class="string">"女"</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="枚举类的成员变量、方法和构造器"><a href="#枚举类的成员变量、方法和构造器" class="headerlink" title="枚举类的成员变量、方法和构造器"></a>枚举类的成员变量、方法和构造器</h2><p>可以将一个枚举类型看作是一个类，它继承于<code>java.lang.Enum</code>类，<strong>当定义一个枚举类型时，每一个枚举类型成员都可以看作是枚举类型的一个实例，这些枚举类型成员默认都被final、public、static所修饰</strong>，所以当<strong>使用枚举类型成员时直接使用枚举类型名称调用</strong>枚举类型成员即可。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Gender</span> {</span><br><span class="line">    MALE,FEMALE;</span><br><span class="line">    <span class="comment">//定义一个public修饰的实例变量</span></span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GenderTest</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="comment">//通过Enum的valueOf()方法来获取指定枚举类的枚举值</span></span><br><span class="line">        <span class="type">Gender</span> <span class="variable">g</span> <span class="operator">=</span> Enum.valueOf(Gender.class, <span class="string">"FEMALE"</span>);<span class="comment">//此时的g也就是枚举值，相当于new产生的对象</span></span><br><span class="line">        <span class="comment">//直接为枚举值的name实例变量赋值</span></span><br><span class="line">        g.name=<span class="string">"女"</span>;</span><br><span class="line">        <span class="comment">//直接访问枚举值的name实例变量</span></span><br><span class="line">        System.out.println(g+<span class="string">"代表"</span>+g.name);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>输出结果如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FEMALE代表女</span><br></pre></td></tr></tbody></table></figure>
<p>这里就要注意<code>Enum</code>类的<code>valueOf()</code>和一个具体枚举类的<code>valueOf()</code>方法的区别了：</p>
<h2 id="Enum类的valueOf-和具体枚举类的valueOf-方法的区别"><a href="#Enum类的valueOf-和具体枚举类的valueOf-方法的区别" class="headerlink" title="Enum类的valueOf()和具体枚举类的valueOf()方法的区别"></a>Enum类的valueOf()和具体枚举类的<code>valueOf()</code>方法的区别</h2><p><code>Enum</code> 类的 <code>valueOf</code> 方法是一个静态方法，用于根据指定的枚举常量名称返回对应的枚举常量的实例。它的具体语法如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Enum.valueOf(Class&lt;T&gt; enumType, String name)</span><br></pre></td></tr></tbody></table></figure>
<p>而一个具体的枚举类的 <code>valueOf</code> 方法是一个非静态方法，也用于返回该枚举常量的实例。它的具体语法如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">aEnum <span class="title function_">valueOf</span><span class="params">(String name)</span></span><br></pre></td></tr></tbody></table></figure>
<p>两个方法的<code>name</code>参数都是表示要获取的枚举常量的名称。</p>
<p>唯一的区别在于，<code>Enum</code> 类的 <code>valueOf</code> 方法多了一个<code>enumType</code>参数指定枚举类型的 <code>Class</code> 对象。</p>
<h2 id="枚举类方法"><a href="#枚举类方法" class="headerlink" title="枚举类方法"></a>枚举类方法</h2><p>Java 中的每一个枚举都继承自 java.lang.Enum 类。当定义一个枚举类型时，<strong>每一个枚举类型成员都可以看作是 Enum 类的实例</strong>，这些枚举成员默认都被 final、public, static 修饰，<strong>当使用枚举类型成员时，直接使用枚举名称调用成员即可</strong>。</p>
<p>下面是<code>Enum</code>类的主要方法：</p>
<div class="table-container">
<table>
<thead>
<tr>
<th>方法名称</th>
<th>描述</th>
<th><strong>使用方法</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>values()</td>
<td>以数组形式返回枚举类型的所有成员</td>
<td>枚举类型名称.values()</td>
</tr>
<tr>
<td>valueOf()</td>
<td>将普通字符串转换为枚举实例</td>
<td>枚举类型名称.valueOf(“ABC”)</td>
</tr>
<tr>
<td>compareTo()</td>
<td>比较两个枚举成员在定义时的顺序</td>
<td>枚举对象.compareTo()</td>
</tr>
<tr>
<td>ordinal()</td>
<td>获取枚举成员的索引位置</td>
<td>枚举对象.ordinal()</td>
</tr>
</tbody>
</table>
</div>
<h3 id="values-方法"><a href="#values-方法" class="headerlink" title="values()方法"></a>values()方法</h3><p> <code>values()</code>方法可以将枚举的所有成员以数组形式返回，也可以通过该方法获取枚举类型的成员。</p>
<p>下面的示例创建一个包含 3 个成员的枚举类型 Signal，然后调用 values() 方法输出这些成员。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Signal</span> {</span><br><span class="line">    <span class="comment">// 定义一个枚举类型</span></span><br><span class="line">    GREEN,YELLOW,RED;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>;i &lt; Signal.values().length;i++) {</span><br><span class="line">        System.out.println(<span class="string">"枚举成员："</span>+Signal.values()[i]);</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 结果如下：</span></span><br><span class="line"><span class="comment">// 枚举成员：GREEN</span></span><br><span class="line"><span class="comment">// 枚举成员：YELLOW</span></span><br><span class="line"><span class="comment">// 枚举成员：RED</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="valueOf-方法与compareTo-方法"><a href="#valueOf-方法与compareTo-方法" class="headerlink" title="valueOf()方法与compareTo()方法"></a>valueOf()方法与compareTo()方法</h3><p><code>valueOf()</code>方法将普通字符串转换为枚举实例（字符串要与枚举值一致这样才能获取到对应的枚举实例）。</p>
<p><code>compareTo()</code>方法用于比较两个枚举对象在定义时的顺序。</p>
<p>下面的例子展示了枚举中<code>valueOf()</code>与<code>compareTo()</code>方法的使用：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Color</span> {</span><br><span class="line">    BLUE,GREEN,RED,WHITE,BLACK;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EnumStudy_1</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">compare</span><span class="params">(Color aColor)</span> {</span><br><span class="line">        <span class="keyword">for</span>(Color i : Color.values()) {</span><br><span class="line">            System.out.println(aColor + <span class="string">"与"</span> + i + <span class="string">"的比较结果为："</span> + aColor.compareTo(i));</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">Color</span> <span class="variable">green</span> <span class="operator">=</span> Color.valueOf(<span class="string">"GREEN"</span>);</span><br><span class="line">        compare(green);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>这个例子的输出结果如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GREEN与BLUE的比较结果为：<span class="number">1</span></span><br><span class="line">GREEN与GREEN的比较结果为：<span class="number">0</span></span><br><span class="line">GREEN与RED的比较结果为：-<span class="number">1</span></span><br><span class="line">GREEN与WHITE的比较结果为：-<span class="number">2</span></span><br><span class="line">GREEN与BLACK的比较结果为：-<span class="number">3</span></span><br></pre></td></tr></tbody></table></figure>
<h3 id="ordinal-方法"><a href="#ordinal-方法" class="headerlink" title="ordinal()方法"></a>ordinal()方法</h3><p>该方法用于得到枚举成员的位置索引。</p>
<p>看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Color</span> {</span><br><span class="line">    BLUE,GREEN,RED,WHITE,BLACK;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">EnumStudy_1</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="keyword">for</span>(Color i : Color.values()) {</span><br><span class="line">            System.out.println(i + <span class="string">"在枚举类的位置索引是："</span> + i.ordinal());</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>输出结果如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">BLUE在枚举类的位置索引是：<span class="number">0</span></span><br><span class="line">GREEN在枚举类的位置索引是：<span class="number">1</span></span><br><span class="line">RED在枚举类的位置索引是：<span class="number">2</span></span><br><span class="line">WHITE在枚举类的位置索引是：<span class="number">3</span></span><br><span class="line">BLACK在枚举类的位置索引是：<span class="number">4</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="枚举类与普通类区别"><a href="#枚举类与普通类区别" class="headerlink" title="枚举类与普通类区别"></a>枚举类与普通类区别</h2><ol>
<li>枚举类可以实现一个或多个接口，<strong>使用<code>enum</code>定义的枚举类默认继承<code>java.lang.Enum</code>类，而不是默认继承<code>Object</code>类，因此枚举类不能显示继承其他父类</strong>，其中 <code>java.lang.Enum</code>类实现了<code>java.lang.Serializable</code>和<code>java.lang.Comparable</code>两个接口。</li>
<li>使用<code>enum</code>定义、非抽象的枚举类默认会使用final修饰，因此<strong>枚举类不能派生子类</strong>。</li>
<li><strong>枚举类的构造器只能使用private访问控制符</strong>，如果省略了构造器的访问控制符，则默认使用private修饰；如果强制指定访问控制符，则只能指定private修饰符。</li>
<li><strong>枚举类的所有实例必须在枚举类的第一行显示列出，否则这个枚举类永远不能产生实例</strong>。列出这些实例时，系统会自动添加public static final修饰，无需我们显式添加。</li>
</ol>
<h1 id="反射"><a href="#反射" class="headerlink" title="反射"></a>反射</h1>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Java%E5%9F%BA%E7%A1%80-%E6%9E%9A%E4%B8%BE%E7%B1%BB-%E5%8F%8D%E5%B0%84/" rel="tag"># -Java基础 -枚举类 -反射</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/05/08/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86%EF%BC%9A%E8%BF%90%E7%AE%97%E6%96%B9%E6%B3%95%E5%92%8C%E8%BF%90%E7%AE%97%E5%99%A8/" rel="prev" title="计算机组成原理：运算方法和运算器">
                  <i class="fa fa-chevron-left"></i> 计算机组成原理：运算方法和运算器
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/05/09/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%8E%9F%E7%90%86%EF%BC%9A%E8%A7%A6%E5%8F%91%E5%99%A8/" rel="next" title="数据库原理：触发器">
                  数据库原理：触发器 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
